const mongoose = require("mongoose")
const { ENV_DOMAIN, ENV_PORT, ENV_COLLECTION } = require('./config')

mongoose.connect(`mongodb://${ENV_DOMAIN}:${ENV_PORT}/${ENV_COLLECTION}`)

module.exports = (success) => {
    mongoose.connection.once('open', async () => {
        console.log('连接成功...')
        success()
        // const bookSchema = mongoose.Schema({
        //     name: String,
        //     author: String,
        //     price: Number
        // })
        // const BookModel = mongoose.model('books', bookSchema)
        // // BookModel.create({ name: '西游记', author: '吴承恩', price: '39.9' })
        // // const xiyou = new BookModel({ name: '钢铁侠', author: '漫威', price: '339.9' })
        // // xiyou.save()
        // // BookModel.deleteOne({ _id: '6634afcd581815936785ae6f' })
        // // console.log(xiyou, 'xiyouooyoy')
        // // const book = await BookModel.deleteOne({ name: '西游记' })
        // // console.log(book, 'bokkk')
        // // const book = await BookModel.updateOne({ name: '西游记' }, { price: 9.9 })
        // // console.log(book, 'bookbookbook')
        // console.log(await BookModel.find({ price: {$lte: 9.9} }))
    })
    
    mongoose.connection.on('error', () => {
        console.log('连接失败')
    })
    
    mongoose.connection.on('close', () => {
        console.log('连接关闭')
    })
}